﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

namespace AdvancedStandingAndScholarship.Views
{
    public partial class AddSuccessPage : Page
    {
        public AddSuccessPage()
        {
            InitializeComponent();
        }
        public static List<ASASService.Student> studentList = new List<ASASService.Student>();
         
        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (CookieHelper.GetCookie("fileID123") != null)
            {
            //declare
            ASASService.ASASServiceClient client = new ASASService.ASASServiceClient();
            //event handler
            client.getStudentByFileIDCompleted += new EventHandler<ASASService.getStudentByFileIDCompletedEventArgs>(client_getStudentByFileIDCompleted);
            //call method
            client.getStudentByFileIDAsync(CookieHelper.GetCookie("fileID123").ToString());
            //client.getStudentByFileIDAsync("88");
            }
            else
            {
                var newStudentList = studentList.OrderBy(x => x.NRIC1).ToList();
                datagrid.ItemsSource = newStudentList;
           }
        }

        void client_getStudentByFileIDCompleted(object sender, ASASService.getStudentByFileIDCompletedEventArgs e)
        {
            if (e.Error == null & e.Result != null)
            {
                studentList = e.Result as List<ASASService.Student>;
                var newStudentList = studentList.OrderBy(x => x.NRIC1).ToList();
                datagrid.ItemsSource = newStudentList;
            }
        }
        

        //done button
        private void btnDone_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // TODO: Add event handler implementation here.
            List<ASASService.Student> studentList = new List<ASASService.Student>();
            studentList = (List<ASASService.Student>)datagrid.ItemsSource;
            List<ASASService.Student> studentListToDelete = new List<ASASService.Student>();
            foreach (KeyValuePair<string, bool> temp in checkedList)
            {
                foreach (ASASService.Student student in studentList)
                {
                    if (temp.Key == student.Student_id)
                    {
                        student.ChkDelete = true;
                        break;
                    }
                }
            }

            ASASService.ASASServiceClient client = new ASASService.ASASServiceClient();
            client.updateAndDeleteStudent1Completed += new EventHandler<ASASService.updateAndDeleteStudent1CompletedEventArgs>(client_updateAndDeleteStudent1Completed);
            client.updateAndDeleteStudent1Async(studentList);
            
        }

        void client_updateAndDeleteStudent1Completed(object sender, ASASService.updateAndDeleteStudent1CompletedEventArgs e)
        {
            if (e.Error == null && e.Result != null)
            {
                MessageBox.Show("Update Success!");
            }
        }

        List<KeyValuePair<string, bool>> checkedList = new List<KeyValuePair<string, bool>>();

        private void cbx_delete_Click(object sender, RoutedEventArgs e)
        {
            CheckBox checkbox = sender as CheckBox;
            if (checkbox.IsChecked.Value == false)
            {
                foreach (KeyValuePair<string, bool> temp in checkedList)
                {
                    if (temp.Key == checkbox.Tag.ToString())
                    {
                        checkedList.Remove(temp);
                        break;
                    }
                }
            }
            else
                checkedList.Add(new KeyValuePair<string, bool>(checkbox.Tag.ToString(), checkbox.IsChecked.Value));
        }

        
    }
}